using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Module.Common.Models;
using Module.Core.Common;

using Module.Core.EntitiesInfo;

namespace Module.Core.DAO
{
    public class ConfigKeywordDAO
    {
        #region Fields

        protected string connectionStringName;

        #endregion

        #region Methods

        /// <summary>
        /// Saves a record to the ConfigKeyword table.
        /// </summary>
        public virtual int Insert(ConfigKeywordInfo configKeywordInfo)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@ConfigIdLang", configKeywordInfo.ConfigIdLang),
				new SqlParameter("@ConfigValue_VN", configKeywordInfo.ConfigValue_VN),
				new SqlParameter("@Status", configKeywordInfo.Status),
				new SqlParameter("@Lang", configKeywordInfo.Lang),
				new SqlParameter("@ConfigValue_Eng", configKeywordInfo.ConfigValue_Eng),
				new SqlParameter("@ConfigKey", configKeywordInfo.ConfigKey)
			};

            return (int)SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "ConfigKeyword_Insert", parameters);
        }

        /// <summary>
        /// Updates a record in the ConfigKeyword table.
        /// </summary>
        public virtual void Update(ConfigKeywordInfo configKeywordInfo)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@ConfigId", configKeywordInfo.ConfigId),
				new SqlParameter("@ConfigIdLang", configKeywordInfo.ConfigIdLang),
				new SqlParameter("@ConfigValue_VN", configKeywordInfo.ConfigValue_VN),
				new SqlParameter("@Status", configKeywordInfo.Status),
				new SqlParameter("@Lang", configKeywordInfo.Lang),
				new SqlParameter("@ConfigValue_Eng", configKeywordInfo.ConfigValue_Eng),
				new SqlParameter("@ConfigKey", configKeywordInfo.ConfigKey)
			};

            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "ConfigKeyword_Update", parameters);
        }

        /// <summary>
        /// Deletes a record from the ConfigKeyword table by its primary key.
        /// </summary>
        public virtual void Delete(int configId)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@ConfigId", configId)
			};

            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "ConfigKeyword_Delete", parameters);
        }

        /// <summary>
        /// Selects a single record from the ConfigKeyword table.
        /// </summary>
        public virtual ConfigKeywordInfo Select(int configId)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@ConfigId", configId)
			};

            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "ConfigKeyword_Select", parameters))
            {
                if (dataReader.Read())
                {
                    return MakeConfigKeywordInfo(dataReader);
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// Selects all records from the ConfigKeyword table.
        /// </summary>
        public CHRTList<ConfigKeywordInfo> SelectAll()
        {
            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "ConfigKeyword_SelectAll"))
            {
                CHRTList<ConfigKeywordInfo> configKeywordInfoList = new CHRTList<ConfigKeywordInfo>();
                while (dataReader.Read())
                {
                    ConfigKeywordInfo configKeywordInfo = MakeConfigKeywordInfo(dataReader);
                    configKeywordInfoList.Add(configKeywordInfo);
                }

                return configKeywordInfoList;
            }
        }

        public virtual void XMLConfig(string fileNameXML, string pathWriteXML, int lang)
        {
            using (DataSet ds = SqlClientUtility.ExecuteDataSet(connectionStringName, CommandType.StoredProcedure, "ConfigKeyword_SelectAll"))
            {
                if (ds.Tables[0].Rows.Count > 0)
                    ds.WriteXml(pathWriteXML + "\\" + fileNameXML + ".xml");
                else

                    ComomFunctions.DeleteFile(pathWriteXML + "\\" + fileNameXML + ".xml");
            }
        }

        /// <summary>
        /// Creates a new instance of the ConfigKeyword class and populates it with data from the specified SqlDataReader.
        /// </summary>
        protected virtual ConfigKeywordInfo MakeConfigKeywordInfo(SqlDataReader dataReader)
        {
            ConfigKeywordInfo configKeywordInfo = new ConfigKeywordInfo();
            configKeywordInfo.ConfigId = SqlClientUtility.GetInt32(dataReader, DbConstants.CONFIGKEYWORD.CONFIGID, 0);
            configKeywordInfo.ConfigIdLang = SqlClientUtility.GetInt32(dataReader, DbConstants.CONFIGKEYWORD.CONFIGIDLANG, 0);
            configKeywordInfo.ConfigValue_VN = SqlClientUtility.GetString(dataReader, DbConstants.CONFIGKEYWORD.CONFIGVALUE_VN, String.Empty);
            configKeywordInfo.Status = SqlClientUtility.GetInt32(dataReader, DbConstants.CONFIGKEYWORD.STATUS, 0);
            configKeywordInfo.Lang = SqlClientUtility.GetInt32(dataReader, DbConstants.CONFIGKEYWORD.LANG, 0);
            configKeywordInfo.ConfigValue_Eng = SqlClientUtility.GetString(dataReader, DbConstants.CONFIGKEYWORD.CONFIGVALUE_ENG, String.Empty);
            configKeywordInfo.ConfigKey = SqlClientUtility.GetString(dataReader, DbConstants.CONFIGKEYWORD.CONFIGKEY, String.Empty);

            return configKeywordInfo;
        }

        #endregion
    }
}
